package io.renren.modules.com.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xiaoleilu.hutool.date.DateUtil;
import com.xiaoleilu.hutool.util.NumberUtil;

import io.renren.common.utils.NumberUtils;
import io.renren.modules.com.dao.ComCrChargingitemDao;
import io.renren.modules.com.entity.ComAItemEntity;
import io.renren.modules.com.entity.ComCollectionRecordEntity;
import io.renren.modules.com.entity.ComCrChargingitemEntity;
import io.renren.modules.com.service.ComAItemService;
import io.renren.modules.com.service.ComCrChargingitemService;

@Service("comCrChargingitemService")
public class ComCrChargingitemServiceImpl implements ComCrChargingitemService {

	@Autowired
	private ComCrChargingitemDao comCrChargingitemDao;

	@Autowired
	private ComAItemService comAItemService;

	@Override
	public ComCrChargingitemEntity queryObject(Long serialNo) {
		return comCrChargingitemDao.queryObject(serialNo);
	}

	@Override
	public List<ComCrChargingitemEntity> queryList(Map<String, Object> map) {
		return comCrChargingitemDao.queryList(map);
	}

	@Override
	public int queryTotal(Map<String, Object> map) {
		List<String> itemList = null;
		if (null != map.get("items") && StringUtils.isNotBlank(map.get("items").toString().trim())
				&& !StringUtils.equals("点击选择...", map.get("items").toString().trim())) {
			itemList = new ArrayList<String>(Arrays.asList(map.get("items").toString().replace(" ", "").split(",")));
		} else {
			itemList = itemList();
		}
		map.put("itemList", itemList);
		return comCrChargingitemDao.queryTotal(map);
	}

	private List<String> itemList() {
		List<String> itemList = new ArrayList<String>();
		itemList.add("租金");
		itemList.add("物业费");
		itemList.add("管理费");
		itemList.add("公共能耗费");
		itemList.add("停车费");
		itemList.add("租场费");
		itemList.add("其他");
		return itemList;
	}

	@Override
	public void save(ComCrChargingitemEntity comCrChargingitem) {
		comCrChargingitemDao.save(comCrChargingitem);
	}

	@Override
	public void update(ComCrChargingitemEntity comCrChargingitem) {
		comCrChargingitemDao.update(comCrChargingitem);
	}

	@Override
	public void delete(Long serialNo) {
		comCrChargingitemDao.delete(serialNo);
	}

	public void deleteByCrSerialNo(Long crSerialNo) {
		comCrChargingitemDao.deleteByCrSerialNo(crSerialNo);
	}

	@Override
	public void deleteBatch(Long[] serialNos) {
		comCrChargingitemDao.deleteBatch(serialNos);
	}

	public void deleteBatchByCrSerialNos(Long[] crSerialNos) {
		comCrChargingitemDao.deleteBatchByCrSerialNos(crSerialNos);
	}

	public List<ComCrChargingitemEntity> queryListByCrSerialNo(Long crSerialNo) {
		return comCrChargingitemDao.queryListByCrSerialNo(crSerialNo);
	}

	public ComCrChargingitemEntity queryByCrSerialNoAndChargingItem(Long crSerialNo, String chargingItem) {
		return comCrChargingitemDao.queryByCrSerialNoAndChargingItem(crSerialNo, chargingItem);
	}

	public void eclipse(ComCrChargingitemEntity comCrChargingitem, ComCollectionRecordEntity comCollectionRecord,
			Map<String, Object> chinaJoyMap) {
		eclipsePrivate(comCrChargingitem, comCollectionRecord, chinaJoyMap);
	}

	private void eclipsePrivate(ComCrChargingitemEntity comCrChargingitemEntity,
			ComCollectionRecordEntity comCollectionRecord, Map<String, Object> chinaJoyMap) {
		Date sd = null;
		Date ed = null;
		Integer month = null;
		Double distance = null;
		ComAItemEntity comAItemEntity = null;
		SimpleDateFormat sdfOne = new SimpleDateFormat("yyyy-MM");
		SimpleDateFormat sdfTwo = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
		comAItemEntity = comAItemService.minMax(comCrChargingitemEntity.getCrSerialNo(),
				comCrChargingitemEntity.getChargingItem());
		if (comAItemEntity == null || comAItemEntity.getItem() == null || comAItemEntity.getYearMonth() == null) {
			chinaJoyMap.put("billingCycle", "");
			chinaJoyMap.put("month", "");
			return;
		}
		if (DateUtil.parse(comCrChargingitemEntity.getChargingSd(), sdfTwo)
				.after(DateUtil.parse(comAItemEntity.getItem(), sdfOne))
				|| StringUtils.equals(comCrChargingitemEntity.getChargingSd().substring(0, 7), comAItemEntity.getItem())
				|| StringUtils.equals(comCrChargingitemEntity.getChargingEd().substring(0, 7),
						comAItemEntity.getYearMonth())) {
			sd = DateUtil.parse(comCrChargingitemEntity.getChargingSd(), sdfTwo);
		} else {
			calendar.setTime(DateUtil.parse(comAItemEntity.getItem(), sdfOne));
			calendar.set(Calendar.DATE, calendar.getActualMinimum(Calendar.DATE));
			sd = calendar.getTime();
		}
		if (DateUtil.parse(comCrChargingitemEntity.getChargingEd(), sdfTwo)
				.after(DateUtil.parse(comAItemEntity.getYearMonth(), sdfOne))
				&& !StringUtils.equals(comCrChargingitemEntity.getChargingEd().substring(0, 7),
						comAItemEntity.getYearMonth())) {
			ed = DateUtil.parse(comCrChargingitemEntity.getChargingEd(), sdfTwo);
		} else {
			calendar.setTime(DateUtil.parse(comAItemEntity.getYearMonth(), sdfOne));
			calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
			ed = calendar.getTime();
		}
		if (DateUtil.parse(comCollectionRecord.getCollectionDays(), sdfTwo)
				.after(DateUtil.parse(comCrChargingitemEntity.getChargingEd(), sdfTwo))
				|| StringUtils.equals(comCollectionRecord.getCollectionDays().substring(0, 7),
						comCrChargingitemEntity.getChargingEd().substring(0, 7))) {
			ed = DateUtil.parse(comCrChargingitemEntity.getChargingEd(), sdfTwo);
		}
		distance = NumberUtil.div(NumberUtil.sub(ed.getTime(), sd.getTime()), NumberUtils.mul(1000, 60, 60, 24, 30));
		month = (int) Math.round(distance);
		if (month < 1) {
			month = 1;
		}
		chinaJoyMap.put("billingCycle", sdfTwo.format(sd) + "至" + sdfTwo.format(ed));
		chinaJoyMap.put("month", month);
	}

	public List<ComCrChargingitemEntity> hasumiKurea(Map<String, Object> map) {
		return comCrChargingitemDao.hasumiKurea(map);
	}

}
